import { Component, OnInit } from '@angular/core';
import { Data } from '@angular/router';
import { AuditlogService, DatamodelService, Model } from 'sparrow-permission-lib';
import { Pageable } from 'src/app/domain/sparrow';
import { GroupService } from 'src/app/service/group.service';
import { LogService } from 'src/app/service/log.service';

@Component({
  selector: 'app-log',
  templateUrl: './log.component.html',
  styleUrls: ['./log.component.css']
})
export class LogComponent implements OnInit {

  pageable: Pageable = {page: 0, size: 10}
  total: number = 0;

  models: Model[]= []
  selectedModel?: string;

  constructor(private logService: LogService,
    private modelService: DatamodelService,
    private auditLogService: AuditlogService
    ) { }

  ngOnInit(): void {
    this.modelService.models({page:0,size:1000000}).subscribe(res=>{
      this.models = res.content!
    })

  }

  listOfCurrentPageData: readonly Data[] = [];


  onCurrentPageDataChange(listOfCurrentPageData: readonly Data[]): void {
    this.listOfCurrentPageData = listOfCurrentPageData;

  }
  onPageIndexChange(page: number) {
    this.pageable.page = page;
    this.logService.list(this.pageable).subscribe(res => {
      this.total = res.totalElements;
      this.listOfCurrentPageData = res.content;
    })
  }

  onPageSizeChange(size: number) {
    this.pageable.size = size;
    this.logService.list(this.pageable).subscribe(res => {
      this.total = res.totalElements;
      this.listOfCurrentPageData = res.content;
    })
  }

  auditLogs: any[] = []
  openChange(e: any){
    console.log(this.selectedModel)
    if(e===false){
      this.auditLogService.getLog1(this.selectedModel!).subscribe(res=>{
        this.total = 100
        this.listOfCurrentPageData = res;
      })
    }
  }

}
